package app.controller.city;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import common.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import app.entity.UserSession;
import app.service.DeviceManageService;
import app.service.FacilitiesManageService;
import app.utils.Common;
import app.utils.DataResponse;
import refactor.service.IUserService;
import siotwx.dao.GroupDao;
import siotwx.entity.Facmonitoring;
import siotwx.entity.Group;

/**
 * 
 * @author 周超 2018年3月15日 
 *
 */
@Controller
public class FacController {
	
	@Autowired
	@Qualifier("FacilitiesManageService")
	private FacilitiesManageService facService;
	
	@Autowired
	@Qualifier("DeviceManageService")
	private DeviceManageService devService;

	@Autowired
	@Qualifier("GroupDao")
	private GroupDao groupDao;

	private IUserService userService;

	@Autowired
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	/**
	 * 获取设备对应开门人
	 *
	 * @param devId
	 * @param response
	 */
	@GetMapping(value="/fac/{devId}/openr")
	public void facOperator(@PathVariable("devId") Integer devId, HttpServletResponse response){
		List<UserVo> userVos = userService.showAccessController(devId);

		DataResponse.jsonResponse(response, userVos);
	}

	/**
	 * 获取当前区域中的所有设施分组
	 *
	 * @param response
	 * @param session
	 */
	@RequestMapping("/{role}/groups")
	public void groups(HttpServletResponse response,HttpSession session) {
		UserSession user = (UserSession) session.getAttribute(Common.sessionUser);
		Map<String, Object> params=new HashMap<>();
		params.put(Group.FieldName.belong_to.toString(), user.getRegionId());
		params.put(Group.FieldName.id.toString(), "0");
		List<Group> groupList = groupDao.getlist(params);
		List<Bean> groups=new ArrayList<>();
		for(Group group:groupList) {
			groups.add(new Bean(group.getId(), group.getGroup_name()));
		}
		DataResponse.jsonResponse(response, groups);
	}

	public class Bean{
		private Integer groupId;
		private String groupName;
		
		public Bean(Integer integer, String groupName) {
			this.groupId = integer;
			this.groupName = groupName;
		}
		public Integer getGroupId() {
			return groupId;
		}
		public void setGroupId(Integer groupId) {
			this.groupId = groupId;
		}
		public String getGroupName() {
			return groupName;
		}
		public void setGroupName(String groupName) {
			this.groupName = groupName;
		}
		
	}

	/**
	 * 获取设备的实时状态
	 *
	 * @param list
	 * @param response
	 */
	@GetMapping("/fac/statuslist/{list}")
	public void facRealtimeStatus(@PathVariable("list") String list,HttpServletResponse response){
		String[] split = list.split(",");
		List<String> devids=new ArrayList<>();
		List<Facmonitoring> updateFacStu=new ArrayList<>();
		if (split!=null&&split.length>0) {
			for(String val:split){
				devids.add(val);
			}
			updateFacStu = facService.updateFacStu(devids);
		}
		DataResponse.jsonResponse(response, updateFacStu);
	}
	
}
